#set( $symbol_pound = '#' )
#set( $symbol_dollar = '$' )
#set( $symbol_escape = '\' )
package ${package}.biz.helper;


import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.cookie.Cookie;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by well on 16/6/20.
 */
public class HttpClientHelper {


    public static HttpResult sendGet(String url, Map<String, String> headers, Map<String, String> params) throws Exception {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        if (params != null && params.size() > 0) {
            url = url + assemblyParameter(params);
        }
        HttpGet httpGet = new HttpGet(url);
        if (null != headers) {
            httpGet.setHeaders(assemblyHeader(headers));
        }
        HttpResponse response = httpclient.execute(httpGet);
        HttpEntity entity = response.getEntity();
        HttpResult httpResult = new HttpResult();
        httpResult.setStatusCode(response.getStatusLine().getStatusCode());
        httpResult.setHeaders(response.getAllHeaders());
        httpResult.setHttpEntity(entity);

        return httpResult;

    }

    public static HttpResult sendPost(String url, Map<String, String> headers, String param) throws Exception {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url);
        if(StringUtils.hasText(param)){
            httpPost.setEntity(new StringEntity(param, "UTF-8"));
        }

        if (null != headers) {
            httpPost.setHeaders(assemblyHeader(headers));
        }
        HttpResponse response = httpclient.execute(httpPost);
        HttpEntity entity = response.getEntity();
        HttpResult httpResult = new HttpResult();
        httpResult.setStatusCode(response.getStatusLine().getStatusCode());
        httpResult.setHeaders(response.getAllHeaders());
        httpResult.setHttpEntity(entity);

        return httpResult;

    }

    public static HttpResult sendPut(String url, Map<String, String> headers, String param) throws Exception {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPut httpPut = new HttpPut(url);
        if(StringUtils.hasText(param)){
            httpPut.setEntity(new StringEntity(param, "UTF-8"));
        }

        if (null != headers) {
            httpPut.setHeaders(assemblyHeader(headers));
        }
        HttpResponse response = httpclient.execute(httpPut);
        HttpEntity entity = response.getEntity();
        HttpResult httpResult = new HttpResult();
        httpResult.setStatusCode(response.getStatusLine().getStatusCode());
        httpResult.setHeaders(response.getAllHeaders());
        httpResult.setHttpEntity(entity);

        return httpResult;

    }


    public static HttpResult sendPost(String url, Map<String, String> headers, Map<String, String> params, String encoding) throws Exception {
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url);
        List<NameValuePair> list = new ArrayList<NameValuePair>();
        for (String temp : params.keySet()) {
            list.add(new BasicNameValuePair(temp, params.get(temp)));
        }

        httpPost.setEntity(new UrlEncodedFormEntity(list, encoding));
        if (null != headers) {
            httpPost.setHeaders(assemblyHeader(headers));
        }
        HttpResponse response = httpclient.execute(httpPost);
        HttpEntity entity = response.getEntity();
        HttpResult httpResult = new HttpResult();
        httpResult.setStatusCode(response.getStatusLine().getStatusCode());
        httpResult.setHeaders(response.getAllHeaders());
        httpResult.setHttpEntity(entity);

        return httpResult;

    }

    public static Header[] assemblyHeader(Map<String, String> headers) {
        Header[] allHeader = new BasicHeader[headers.size()];
        int i = 0;
        for (String str : headers.keySet()) {
            allHeader[i] = new BasicHeader(str, headers.get(str));
            i++;
        }

        return allHeader;

    }


    public static String assemblyCookie(List<Cookie> cookies) {
        StringBuffer sbu = new StringBuffer();
        for (Cookie cookie : cookies) {
            sbu.append(cookie.getName()).append("=").append(cookie.getValue()).append(";");
        }
        if (sbu.length() > 0) sbu.deleteCharAt(sbu.length() - 1);
        return sbu.toString();

    }

    public static String assemblyParameter(Map<String, String> parameters) {
        String para = "?";
        for (String str : parameters.keySet()) {
            para += str + "=" + parameters.get(str) + "&";
        }
        return para.substring(0, para.length() - 1);

    }

}
